package com.etime.yqtsponsor.fileupload;

import com.etime.yqtsponsor.dao.ActivePicBankMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.UUID;

/**
 * @author jj
 * @date2022/7/26 11:41
 *
 * 图片上传
 */

@RestController
public class FileUploadController {
    @Value("${file-save-path}")
    private String fileSavePath;
    @Autowired
    private ActivePicBankMapper activePicBankMapper;
    ArrayList<String> mylist=new ArrayList();
    SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd/");

    @PostMapping("/upload")
    public String upload(MultipartFile uploadFile, HttpServletRequest req) {
        String filePath = "";
        String format = sdf.format(new Date());
        //拿到图片的类型判断 后面要加上去
        File folder = new File(fileSavePath + format);
        if (!folder.isDirectory()) {
            folder.mkdirs();
            String oldName = uploadFile.getOriginalFilename();
            //判断图片格式类型
            String lastName = oldName.substring(oldName.lastIndexOf("."), oldName.length());
            if (lastName.equals("jpg") || lastName.equals("jpeg") || lastName.equals("png") || lastName.equals("gif") || lastName.equals("bmp") || lastName.equals("JPG")) {
                String newName = UUID.randomUUID().toString() + lastName;
                try {
                    uploadFile.transferTo(new File(folder, newName));
                    filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploads/wechat/" + format + newName;
                } catch (IOException e) {
                    e.printStackTrace();
                    return "上传失败";
                }
            }
        } else {
            return "图片上传格式不合法！！！";
        }
        String oldName = uploadFile.getOriginalFilename();
        String lastName = oldName.substring(oldName.lastIndexOf("."), oldName.length());
        String newName = UUID.randomUUID().toString() + lastName;
        try {
            uploadFile.transferTo(new File(folder, newName));
            filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploads/wechat/" + format + newName;
        } catch (IOException e) {
            e.printStackTrace();
            return "上传失败";
        }
        return filePath;
    }


    //多图片上传
    @PostMapping("/uploadPicList")
    public ArrayList uploadPicList(MultipartFile[] uploadFiles, HttpServletRequest req){
        String filePath = "";
        for (MultipartFile uploadFile : uploadFiles){
            String format = sdf.format(new Date());
            File folder = new File(fileSavePath+format);
            if (!folder.isDirectory()){
                folder.mkdirs();
                String oldName = uploadFile.getOriginalFilename();
                //判断图片格式类型
                String lastName = oldName.substring(oldName.lastIndexOf("."), oldName.length());
                if (lastName.equals("jpg") || lastName.equals("jpeg") || lastName.equals("png") || lastName.equals("gif") || lastName.equals("bmp") || lastName.equals("JPG")) {
                    String newName = UUID.randomUUID().toString() + lastName;
                    try {
                        uploadFile.transferTo(new File(folder, newName));
                        filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploads/wechat/" + format + newName;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }

            String oldName = uploadFile.getOriginalFilename();
            String lastName = oldName.substring(oldName.lastIndexOf("."), oldName.length());
            String newName = UUID.randomUUID().toString() + lastName;
            try {
                uploadFile.transferTo(new File(folder, newName));
                filePath = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + "/uploads/wechat/" + format + newName;
            } catch (IOException e) {
                e.printStackTrace();
            }
            mylist.add(filePath);
        }
        return mylist;
    }
}
